﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SMSManage
{
    public partial class frmManagerMark : Form
    {
        public frmManagerMark()
        {
            InitializeComponent();
        }
        private int MarkID;
        private void frmManagerMark_Load(object sender, EventArgs e)
        {
            //this.tbl_MarkTableAdapter.Fill_tbl_Mark_All(this.mark.tbl_Mark);
            BindData();
        }
        private void BindData()
        {
            //this.tbl_MarkTableAdapter.Fill_tbl_Mark_All(this.mark.tbl_Mark);
            this.cbbStatus.SelectedItem = "Kích hoạt";
            this.txtClassName.Text = String.Empty;
            this.txtSubject.Text = String.Empty;
            this.txtAccount.Text = String.Empty;
            this.tstDescription.Text = String.Empty;
            
            this.dtpkDate.Value = DateTime.Now;
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                try
                {
                    this.dataGridView1.Rows[i].Cells[0].Value = (i + 1).ToString();
                    this.dataGridView1.Rows[i].Cells["MarkDescription"].Value = "...";
                    this.dataGridView1.Rows[i].Cells["MarkTime"].Value = "...";
                }
                catch (Exception exc)
                {
                    exc.ToString();
                }
            }
            this.dataGridView1.Refresh();
            //DatasetXSD.MarkTableAdapters.tbl_MarkTableAdapter dpMark = 
            //    new SMSManage.DatasetXSD.MarkTableAdapters.tbl_MarkTableAdapter();                        
            //this.dataGridView1.DataSource = dpMark.Get_tbl_Mark_By_MarkID(MarkID);
            //this.dataGridView1.DataMember = "MarkID";
            ////this.dataGridView1.DataBindings= "";

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1)
                {
                    //Enable buttons
                    this.btnUpdate.Enabled = true;
                    this.btnDel.Enabled = true;

                    //disable buttons
                    this.btnAdd.Enabled = false;
                    this.btnSearch.Enabled = false;
                    MarkID = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["MarkID"].Value);

                    //Disable ID text field

                    this.txtClassName.Text = this.dataGridView1.Rows[e.RowIndex].Cells["ClassName"].Value.ToString();
                    this.txtSubject.Text = this.dataGridView1.Rows[e.RowIndex].Cells["SubjectName"].Value.ToString();
                    this.txtAccount.Text = this.dataGridView1.Rows[e.RowIndex].Cells["StudentName"].Value.ToString();
                    this.tstDescription.Text = this.dataGridView1.Rows[e.RowIndex].Cells["MarkDescription"].Value.ToString();
                    this.dtpkDate.Value = Convert.ToDateTime(this.dataGridView1.Rows[e.RowIndex].Cells["MarkTime"].Value);

                    //change selected value in combobox
                    switch (this.dataGridView1.Rows[e.RowIndex].Cells["MarkStatus"].Value.ToString())
                    {
                        case "1":
                            this.cbbStatus.SelectedItem = "Kích hoạt";
                            break;
                        case "0":
                            this.cbbStatus.SelectedItem = "Khóa";
                            break;
                        default:
                            this.cbbStatus.SelectedItem = "Kích hoạt";
                            break;
                    }
                }

            }
            catch (Exception exc)
            {
                exc.ToString();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
           try
            {
                if (MessageBox.Show("Bạn có thực sự muốn thêm mới học viên này?", "Xác nhận",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    DatasetXSD.MarkTableAdapters.tbl_MarkTableAdapter dp = 
                        new DatasetXSD.MarkTableAdapters.tbl_MarkTableAdapter();

                    int status = 0;
                    switch (this.cbbStatus.SelectedItem.ToString())
                    {
                        case "Kích hoạt":
                            status = 1;
                            break;
                        case "Khóa":
                            status = 0;
                            break;
                        default:
                            status = 1;
                            break;
                    }
                    int ok = dp.Insert_tbl_Mark_Query(Convert.ToDateTime(this.dtpkDate.Value), this.tstDescription.Text.Trim(), status);
                    if (ok > 0)
                    {
                        //Báo thành công
                        MessageBox.Show("Thêm mới thành công", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        BindData();
                        this.dataGridView1.Refresh();
                    }
                    else
                    { 
                        //Báo thất bại
                        MessageBox.Show("Thêm mới thất bại", "Thất bại", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                }
            }
            catch (Exception exc)
            {
                exc.ToString();
                MessageBox.Show("Có lỗi xảy ra, thêm mới thất bại", "Có lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }      
        }

    }
}
